package com.yswl.yeeduo.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

/**
 * GET方法需要用？将参数连接在URL后面，各个参数之间用&连接。 　POST方法发送请求时，仍然使用基本的URL，将参数信息放在请求实体中发送。
 * 
 * @author yunshuwanli
 *
 */

public class KHttpUtils {

	public static final String TAG = KHttpUtils.class.getSimpleName();
	private static CookieStore cookieStore;
	private static String JSESSIONID = null;

	public static String sendPostMethod(String path, Map<String, Object> params, String encoding) throws ClientProtocolException, IOException {
		String result = null;
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		// 参数取出重新配置
		for (Map.Entry<String, Object> entry : params.entrySet()) {
			BasicNameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
			parameters.add(pair);
		}

		UrlEncodedFormEntity fromEntity = null;
		DefaultHttpClient client = null;
		try {
			fromEntity = new UrlEncodedFormEntity(parameters, encoding);
			client = new DefaultHttpClient();
			HttpPost post = new HttpPost(path);

			if (null != JSESSIONID) {
				post.setHeader("Cookie", "JSESSIONID=" + JSESSIONID);
			}
			post.setEntity(fromEntity);
			HttpConnectionParams.setConnectionTimeout(client.getParams(), 30 * 1000);
			HttpResponse response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(response.getEntity(), encoding);
			}

			cookieStore = client.getCookieStore();
			List<Cookie> cookieList = cookieStore.getCookies();
			for (int i = 0; i < cookieList.size(); i++) {
				if ("JSESSIONID".equalsIgnoreCase(cookieList.get(i).getName())) {
					JSESSIONID = cookieList.get(i).getValue();
					break;
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} finally {
			if (client != null) {
				client.getConnectionManager().shutdown();
			}
		}
		return result;
	}

	public static String sendGetMethod(String url, Map<String, Object> params, String encoding) throws ClientProtocolException, IOException {
		String __result = null;
		if (params != null) {
			// 参数取出重新配置
			for (Map.Entry<String, Object> _entry : params.entrySet()) {
				if (url.contains("?")) {
					url += "?" + _entry.getKey() + "=" + _entry.getValue().toString();
				} else {
					url += "&" + _entry.getKey() + "=" + _entry.getValue().toString();
				}
			}
		}

		DefaultHttpClient client = null;
		try {
			client = new DefaultHttpClient();
			HttpGet __get = new HttpGet(url);

			if (null != JSESSIONID) {
				__get.setHeader("Cookie", "JSESSIONID=" + JSESSIONID);
			}
			HttpConnectionParams.setConnectionTimeout(client.getParams(), 30 * 1000);
			HttpResponse _response = client.execute(__get);
			if (_response.getStatusLine().getStatusCode() == 200) {
				__result = EntityUtils.toString(_response.getEntity(), encoding);
			}

			cookieStore = client.getCookieStore();
			List<Cookie> __cookieList = cookieStore.getCookies();
			for (int i = 0; i < __cookieList.size(); i++) {
				if ("JSESSIONID".equalsIgnoreCase(__cookieList.get(i).getName())) {
					JSESSIONID = __cookieList.get(i).getValue();
					break;
				}
			}
		} finally {
			if (client != null) {
				client.getConnectionManager().shutdown();
			}
		}
		return __result;
	}
}
